package com.tanger.es.api;

import com.tanger.common.exception.AbstractController;
import com.tanger.common.result.ActionResult;
import com.tanger.common.utils.StringUtil;
import com.tanger.es.ESConstant;
import com.tanger.es.EsGoods;
import com.tanger.es.inner.Goods;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.functionscore.FunctionScoreQueryBuilder;
import org.elasticsearch.index.query.functionscore.ScoreFunctionBuilders;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * @Description TODO
 * @Author Hchenbin
 * @Date 2024-01-05 16:09
 * @Version 1.0
 */
@RestController
@RequestMapping("/tanger/api/es")
@Api(value = "搜索:商品",tags="搜索:商品")
public class EsController extends AbstractController {

    @Resource
    private ESQueryService service;

    @PostMapping(value = "/search")
    @ApiOperation(value="查询商品", notes="查询商品")
    public ActionResult<List<EsGoods>> search(@RequestBody QueryVO query){
        try {
            if (Objects.isNull(query)
                    || StringUtil.isNull(query.getPageNum()) || StringUtil.isNull(query.getPageSize())) {
                ActionResult.buildResultError("参数缺失！");
            }
            return ActionResult.buildResultSuccess(service.matchPhraseQuery("zh", query));
        } catch (Exception e) {
            e.printStackTrace();
            return ActionResult.buildResultError(e.getMessage());
        }
    }
}
